<script>
function getJSDateTime(d){
    // padding function
    var s = function(a,b){return(1e15+a+"").slice(-b)};

    // default date parameter
    if (typeof d === 'undefined'){
        d = new Date();
    };

    // return ISO datetime
    return s(d.getMonth()+1,2) + '/' +
    	s(d.getDate(),2) + '/' +
		d.getFullYear() + ' ' +
        s(d.getHours(),2) + ':' +
        s(d.getMinutes(),2);// + ':' +
        //s(d.getSeconds(),2);
}
function getISODateTime(d){
	    // padding function
	    var s = function(a,b){return(1e15+a+"").slice(-b)};

	    // default date parameter
	    if (typeof d === 'undefined'){
	        d = new Date();
	    };

	    // return ISO datetime
	    return s(d.getDate(),2) + '/' +
	    	s(d.getMonth()+1,2) + '/' +
    		d.getFullYear() + ' ' +
	        s(d.getHours(),2) + ':' +
	        s(d.getMinutes(),2);// + ':' +
	        //s(d.getSeconds(),2);
	}
</script>
<?php
/* @var $this CalendarController */

$this->breadcrumbs=array(
	'Calendar',
);
date_default_timezone_set('Europe/Paris');



include("teacherNewEventDlg.php");
include("editTeacherEventDlg.php");
?>


<script>
	

	$(document).ready(function() {
		 $('#calendar').fullCalendar({
			header: {
				left: 'prev,next today',
				center: 'title',
				right: 'month,agendaWeek,agendaDay'
			},
			titleFormat: {
		        month: 'MMMM yyyy',
		        week: "d [ MMMM][ yyyy] {'&#8212;' d MMMM yyyy}",
		        day: 'dddd'
		    },
		    timeFormat: 'H:mm',
			selectable: true,
			selectHelper: true,
			disableDragging: true,
			disableResizing: true,
			allDaySlot: false,
			select: function(start, end, jsEvent, view) {
				$("#new-event-station").html('');
				
				if( start.getHours()===0 && end.getHours()===0 ){
					start.setHours(8);
					end.setHours(9);	
				}
					
				$('#startdate').val(getISODateTime(start));
				$('#enddate').val(getISODateTime(end));
				$( "#new-event-dlg" ).dialog("open");
				$.post(
					'<?php echo Yii::app()->request->baseUrl;?>/index.php/calendar/availableStationsForTeacher',
					{
						day : start.getDate(),
						month : start.getMonth()+1,
						year : start.getFullYear(),
						hourStart: 0,
						hourStop: 23,
						minStart: 0,
						minStop: 59,
					},
					readStations,
					"json"
				);
		    },
			eventClick: function(calEvent, jsEvent, view) {
				if ( calEvent.ownerType == 3 ) {
					$("#edit-event-station").html('');
					var toolsdiv = $("#edit-event-avail-tools");				

					$('#edit-event-form .startdate').val(getISODateTime(calEvent.start));
					$('#edit-event-form .enddate').val(getISODateTime(calEvent.end));
					
					$('#edit-event-form .id').val(calEvent.id);
					$('#edit-event-form .eventname').val(calEvent.title);
					$('#edit-event-form .stationname').val(calEvent.station);
					$('#edit-event-form .limit').val(calEvent.limit);
					
					$("#edit-event-dlg" ).dialog("open");
					$.post(
						'<?php echo Yii::app()->request->baseUrl;?>/index.php/calendar/availableStationsForTeacher',
						{
							day : calEvent.start.getDate(),
							month : calEvent.start.getMonth()+1,
							year : calEvent.start.getFullYear(),
							hourStart:calEvent.start.getHours(),
							hourStop:calEvent.end.getHours(),
							minStart:calEvent.start.getMinutes(),
							minStop:calEvent.end.getMinutes(),
							id:calEvent.id
						},
						readStations,
						"json"
					);
				}
		        

		    },
			eventDrop: function(event, delta) {
				//alert(event.title + ' was moved ' + delta + ' days\n' +
				//	'(should probably update your database)');
			},
			eventRender: function(event, element, view) {
				if ( view.name == 'month' ) {
					if ( event.ownerType == 2 ) {
						element.remove();
					}
					else {
						element.addClass("teacher-reservation");
						element.children('.fc-event-inner').append(
							'<div>Limit studentów: '+event.limit+'</div>'
						);
					}
				}
				else {
					if ( event.ownerType == 2 ) {
						element.removeClass("teacher-reservation");
						element.children('.fc-event-inner').append(
							'<div>Opis: '+event.description+'</div>'
						);
					}
					else {
						element.addClass("teacher-reservation");
						element.children('.fc-event-inner').append(
							'<div>Limit studentów: '+event.limit+'</div>'
						);
					}				
				}
		    },
			editable: true,
			events: '<?php echo Yii::app()->request->baseUrl;?>/index.php/calendar/events'
		});
		
	});

	

</script>
<h1>Harmonogram</h1>
<div id='calendar' class='calendar'></div>